<template>
  <div class="container">
    <div class="app-container">
      <el-tree :expand-on-click-node="false" default-expand-all :data="depts" :props="defaultProps">
        <template #default="{data}">
          <el-row style="width: 100%; height: 40px;" align="middle" type="flex" justify="space-between">
            <span>{{ data.name }}</span>
            <el-col :span="4">
              <span class="manager-name">{{ data.managerName }}</span>
              <el-dropdown @command="handleCommand(data.id,$event)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <add-dept ref="addDept" :current-node-id="currentNodeId" :show-dialog.sync="showDialog" @updateDepartment="getDepartmentList" />
  </div>
</template>
<script>
import { getDepartmentList, deleteDepartment } from '@/api/department'
import { listToTree } from '@/utils'
import AddDept from './components/add-dept'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      showDialog: false,
      currentNodeId: null,
      depts: [
        // { name: '传智教育',
        //   managerName: '管理员',
        //   children: [
        //     { name: '总裁办', managerName: '管理员' },
        //     { name: '行政部', managerName: '文吉星' },
        //     { name: '人事部',
        //       managerName: '周乐天',
        //       children: [
        //         { name: '财务核算部', managerName: '周杰伦' },
        //         { name: '税务管理部', managerName: '周杰伦' },
        //         { name: '薪资管理部', managerName: '周杰伦' }
        //       ]
        //     }
        //   ]
        // }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      const res = await getDepartmentList()
      console.log(res)
      // this.depts = res
      this.depts = listToTree(res, 0)
    },
    handleCommand(id, type) {
      console.log(id, type)
      if (type === 'add') {
        this.showDialog = true
        this.currentNodeId = id
      } else if (type === 'edit') {
        this.showDialog = true
        this.currentNodeId = id
        this.$nextTick(() => {
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        this.$confirm('确定删除此部门吗').then(async() => {
          await deleteDepartment(id)
          this.$message.success('删除成功')
          this.getDepartmentList()
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
     .app-container{
      padding:32px 150px;
     }
     .manager-name{
        //  margin-right:20px;
         width: 90px;
         display: inline-block;
     }
</style>
